package com.ymttest.business.service.YMTTrading;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;

import com.google.common.collect.Lists;
import com.ymatou.iapi.optrade.parameter.enums.PromotionTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.vo.CatalogApportionVo;
import com.ymatou.iapi.optrade.parameter.vo.CouponVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.parameter.vo.PrepaidCardVo;
import com.ymatou.iapi.optrade.parameter.vo.SellerPromotionVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.PrepaidCardService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;

public class TradingPlaceEngageOrderCallService {

	private static Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
	private static String sellerName = String.valueOf(EnvSetup.getData("autotest_sellerName1"));
	
	/**
	 * 根据bookingConfirmOrderIds 下单
	 * 
	 * @param bookingConfirmOrderIds
	 *            预定订单列表
	 * @return
	 */
	public static TradingServiceResp placeOrderWithBookingConfirmOrderIds(
			List<Long> bookingConfirmOrderIds) {

		PlaceOrderReq placeorderBean = TradingCallService
				.buildStandPlaceOrderReq();
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		// 创建一个普通商品
		TradingProductVo product = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		
		OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
				product.getProductId(), product.getCatalogId());
		
		orderVo.setOrderItems(Arrays.asList(orderItemVo));
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		
		orderGroupVo.setBookingConfirmOrderIds(bookingConfirmOrderIds);

		placeorderBean.setOrderGroups(Arrays.asList(orderGroupVo));
		
		return TradingCallService.callPlaceOrderService(placeorderBean);
	}
	
	/**
	 * 创建一个定金订单
	 * 
	 * @param usePrecreditCard
	 *            是否使用预付卡
	 */
	public static TradingServiceResp placeEngageOrder(boolean usePrecreditCard) {

		return placeEngageOrder(usePrecreditCard,
				Long.valueOf(EnvSetup.getData("BuyUserId")));
	}

	/**
	 * 创建一个定金订单
	 * 
	 * @param usePrecreditCard
	 *            是否使用预付卡
	 * @param userId
	 *            用户id
	 */
	public static TradingServiceResp placeEngageOrder(boolean usePrecreditCard,
			long userId) {

		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		// 创建一个普通商品
		TradingProductVo product = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		
		OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
				product.getProductId(), product.getCatalogId());
		
		orderItemVo.setEarnestAmount(BigDecimal.ONE);
		orderItemVo.setBookingPromotionAmount(new BigDecimal("20.00"));

		orderVo.setPaymentStartTime(DateTime.now().plusDays(1).toDate());
		orderVo.setPaymentEndTime(DateTime.now().plusDays(2).toDate());

		if (usePrecreditCard) {

			PrepaidCardVo prepaidCardVo = new PrepaidCardVo();

			prepaidCardVo.setCardAmount(BigDecimal.ONE);
			prepaidCardVo.setCardNo(PrepaidCardService.cardId1);

			placeOrderReq.setPrepaidCardVos(Arrays.asList(prepaidCardVo));
		}
		
		orderVo.setOrderItems(Arrays.asList(orderItemVo));
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

		return TradingCallService.callPlaceOrderService(placeOrderReq);
	}

	/**
	 * 单个买手,多商品,生成多个定金订单
	 * 
	 * @param usePrecreditCard
	 *            是否使用预付卡
	 */
	public static TradingServiceResp placeEngageOrder2Order(boolean usePrecreditCard) {

		return placeEngageOrder2Order(usePrecreditCard,
				Long.valueOf(EnvSetup.getData("BuyUserId")));
	}

	/**
	 * 单个买手,多商品,生成多个定金订单
	 * 
	 * @param usePrecreditCard
	 *            是否使用预付卡
	 * @param userId
	 *            用户id
	 * @return
	 */
	public static TradingServiceResp placeEngageOrder2Order(
			boolean usePrecreditCard, long userId) {

		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo1 = TradingCallService.createOrderVo();
		
		// 创建一个普通商品
		TradingProductVo product = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		
		OrderItemVo orderItemVo1 = TradingCallService.createOrderItemVo(
				product.getProductId(), product.getCatalogId());

		orderItemVo1.setBookingPromotionAmount(BigDecimal.TEN);
		orderItemVo1.setEarnestAmount(BigDecimal.ONE);
		
		orderVo1.setOrderItems(Arrays.asList(orderItemVo1));
		
		Date paymentStartTime = DateTime.now().plusDays(1).toDate();
		Date paymentEndTime = DateTime.now().plusDays(2).toDate();

		orderVo1.setPaymentStartTime(paymentStartTime);
		orderVo1.setPaymentEndTime(paymentEndTime);

		// 第二个订单
		OrderVo orderVo2 = TradingCallService.createOrderVo();
		
		OrderItemVo orderItemVo2 = TradingCallService.createOrderItemVo(
				product.getProductId(), product.getCatalogId());
		
		orderItemVo2.setBookingPromotionAmount(BigDecimal.TEN);
		orderItemVo2.setEarnestAmount(BigDecimal.ONE);

		orderVo2.setPaymentStartTime(paymentStartTime);
		orderVo2.setPaymentEndTime(paymentEndTime);
		
		orderVo2.setOrderItems(Arrays.asList(orderItemVo2));

		if (usePrecreditCard) {

			PrepaidCardVo prepaidCardVo = new PrepaidCardVo();

			prepaidCardVo.setCardAmount(BigDecimal.ONE);
			prepaidCardVo.setCardNo(PrepaidCardService.cardId1);

			placeOrderReq.setPrepaidCardVos(Arrays.asList(prepaidCardVo));
		}
		
		orderGroupVo.setOrders(Arrays.asList(orderVo1, orderVo2));
		
		placeOrderReq.setPlaceOrderSource("定金订单");
		placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

		return TradingCallService.callPlaceOrderService(placeOrderReq);
	}

	/**
	 * 单个买手,多商品,生成多个定金订单
	 * 
	 * @param usePrecreditCard
	 *            是否使用预付卡
	 * @param sellerId
	 *            买手id
	 * @param sellerName
	 *            买手名称
	 * @return
	 * @throws Exception 
	 */
	public static PlaceOrderReq buildEngagePlaceOrderReq(
			boolean usePrecreditCard, long sellerId, String sellerName) throws Exception {

		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();
		
		Long userId = (long) new OrderCallService().createNewUser();
		placeOrderReq.setUserId(userId);
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo1 = TradingCallService.createOrderVo();
		
		Date paymentStartTime = DateTime.now().plusDays(1).toDate();
		Date paymentEndTime = DateTime.now().plusDays(2).toDate();
		
		// 创建一个普通商品
		TradingProductVo product1 = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		
		OrderItemVo orderItemVo1 = TradingCallService.createOrderItemVo(
				product1.getProductId(), product1.getCatalogId());
		
		orderItemVo1.setBookingPromotionAmount(BigDecimal.TEN);
		orderItemVo1.setEarnestAmount(BigDecimal.ONE);
		
		orderVo1.setOrderItems(Arrays.asList(orderItemVo1));

		orderVo1.setPaymentStartTime(paymentStartTime);
		orderVo1.setPaymentEndTime(paymentEndTime);

		// 第二个订单
		OrderVo orderVo2 = TradingCallService.createOrderVo();
		
		// 创建一个普通商品
		TradingProductVo product2 = TradingProductVo.createPro(sellerId,
				sellerName, 100);

		OrderItemVo orderItemVo2 = TradingCallService.createOrderItemVo(
				product2.getProductId(), product2.getCatalogId());
		
		orderItemVo2.setBookingPromotionAmount(BigDecimal.TEN);
		orderItemVo2.setEarnestAmount(BigDecimal.ONE);

		orderVo2.setPaymentStartTime(paymentStartTime);
		orderVo2.setPaymentEndTime(paymentEndTime);
		
		orderVo2.setOrderItems(Arrays.asList(orderItemVo2));

		if (usePrecreditCard) {

			PrepaidCardVo prepaidCardVo = new PrepaidCardVo();

			prepaidCardVo.setCardAmount(BigDecimal.ONE);
			prepaidCardVo.setCardNo(PrepaidCardService.cardId1);

			placeOrderReq.setPrepaidCardVos(Arrays.asList(prepaidCardVo));
		}
		
		orderGroupVo.setOrders(Arrays.asList(orderVo1, orderVo2));
		placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

		return placeOrderReq;
	}
	
	/**
	 * 二次确认下单
	 * 
	 * @param userId
	 *            用户id
	 * @param orderId
	 *            订单id
	 */
	public static TradingServiceResp placeFullPaidOrder(long userId,
			long orderId) {

		List<Long> bookingConfirmOrderIds = new ArrayList<Long>();
		bookingConfirmOrderIds.add(orderId);

		return TradingPlaceEngageOrderCallService
				.placeOrderWithBookingConfirmOrderIds(bookingConfirmOrderIds);
	}

	/**
	 * 定金订单二次确认下单，使用商家+平台优惠，使用预付卡，使用商家促销信息
	 * 
	 * @param placeOrderResp
	 */
	public static TradingServiceResp placeFullPaidOrderWithAllPromotion(
			TradingServiceResp placeOrderResp) {

		OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
		PlaceOrderReq placeOrderReq = new PlaceOrderReq();

		Long userId = placeOrderResp.getUserId();
		long tradingId = placeOrderResp.getTradingId();
		List<com.ymatou.iapi.optrade.resp.Order> orderList = placeOrderResp
				.getOrderList();

		List<OrderDetailPo> orderDetailList = new ArrayList<OrderDetailPo>();

		// 支付定金
		TradingCallService.payOrder(userId, placeOrderResp.getPayableAmount(),
				tradingId, 2);
		List<Long> bookingConfirmOrderIds = new ArrayList<Long>();

		for (com.ymatou.iapi.optrade.resp.Order order : orderList) {

			bookingConfirmOrderIds.add(order.getOrderId());

			OrderDetailPo po = orderShardingWapper.selectOrderDetailbyOrderId(
					order.getOrderId()).get(0);
			orderDetailList.add(po);
		}

		placeOrderReq.setAddressId(772208L);
		placeOrderReq.setUserId(userId);
		placeOrderReq.setMainOrderId(TradingCallService
				.genMainOrderId(placeOrderReq.getUserId()));

		OrderGroupVo groupVo1 = new OrderGroupVo();
		groupVo1.setSellerId(sellerId);
		groupVo1.setSellerLoginId(sellerName);
		groupVo1.setBookingConfirmOrderIds(bookingConfirmOrderIds);

		// 使用买手优惠券
		long sellerId = groupVo1.getSellerId();

		CouponVo sellerCoupon1 = TradingAllPromotion.createSellerCouponVo(
				userId, sellerId, new BigDecimal(30), BigDecimal.TEN);

		groupVo1.setSellerCoupon(sellerCoupon1);

		// 定义买手营销信息list
		List<SellerPromotionVo> sellerPromotionList = new ArrayList<SellerPromotionVo>();

		SellerPromotionVo sellerPromotion1 = new SellerPromotionVo();

		sellerPromotion1.setAmountCondition(new BigDecimal(50));
		sellerPromotion1.setId(123456789);
		sellerPromotion1.setName("自动化商家优惠活动");

		sellerPromotion1.setPromotionValue(new BigDecimal(12));
		sellerPromotion1.setQtyCondition(4);

		sellerPromotion1.setReducedAmount(new BigDecimal(15));
		sellerPromotion1.setType(PromotionTypeEnum.ReduceIfAmountEnough);

		sellerPromotion1.setApportions(Arrays.asList(new CatalogApportionVo(
				orderDetailList.get(0).getCatalogId(), orderDetailList.get(0)
						.getProductCount()), new CatalogApportionVo(
				orderDetailList.get(1).getCatalogId(), orderDetailList.get(1)
						.getProductCount())));

		sellerPromotionList.add(sellerPromotion1);

		groupVo1.setSellerPromotionApportions(sellerPromotionList);

		// 使用平台优惠券
		CouponVo ymatouCoupon = TradingAllPromotion.createYMTCouponVo(userId,
				new BigDecimal(60), BigDecimal.TEN);
		placeOrderReq.setYmatouCoupon(ymatouCoupon);

		placeOrderReq.setOrderGroups(Lists.newArrayList(groupVo1));
		placeOrderReq.setAppId("ymt autotest");
		placeOrderReq.setPlaceOrderSource("二次确认订单");

		return TradingCallService.callPlaceOrderService(placeOrderReq);
	}

	/**
	 * 定金订单二次确认下单
	 * 
	 * @param orderList
	 */
	public static TradingServiceResp placeFullPaidOrder(
			List<com.ymatou.iapi.optrade.resp.Order> orderList) {

		List<Long> bookingConfirmOrderIds = new ArrayList<Long>();

		for (com.ymatou.iapi.optrade.resp.Order order : orderList) {

			bookingConfirmOrderIds.add(order.getOrderId());
		}

		PlaceOrderReq placeOrderReq = new PlaceOrderReq();

		placeOrderReq.setAddressId(772208L);
		placeOrderReq.setUserId(orderList.get(0).getOrderPo().getUserId());
		placeOrderReq.setMainOrderId(TradingCallService
				.genMainOrderId(placeOrderReq.getUserId()));
		OrderGroupVo groupVo1 = new OrderGroupVo();

		groupVo1.setSellerId(Long.valueOf(EnvSetup.getData("autotest_seller1")));
		groupVo1.setSellerLoginId(String.valueOf(EnvSetup
				.getData("autotest_sellerName1")));

		groupVo1.setBookingConfirmOrderIds(bookingConfirmOrderIds);
		placeOrderReq.setOrderGroups(Lists.newArrayList(groupVo1));
		placeOrderReq.setAppId("ymt autotest");

		return TradingCallService.callPlaceOrderService(placeOrderReq);
	}
}
